from fastapi import APIRouter, Depends, status
from sqlalchemy.ext.asyncio import AsyncSession

from database.database import get_db
from core.base.api_mode import ApiResponse, api_success
from core.pagination import DependsPagination, PageData

from modules.case_qc.services.case_qc_service import CaseQcService
from modules.case_qc.schemas.case_qc_schema import CaseQcCreate, CaseQcUpdate
from modules.case_qc.params.case_qc_params import CaseQcQuery
from modules.case_qc.vos.case_qc_vo import CaseQcResponse


router = APIRouter()


@router.post(
    "/list",
    response_model=ApiResponse[PageData[CaseQcResponse]],
    summary="获取案情质量表列表",
    dependencies=[DependsPagination],
)
async def list_case_qcs(query_params: CaseQcQuery, db: AsyncSession = Depends(get_db)):
    result = await CaseQcService.get_list_with_pagination(db, query_params)
    return api_success(data=result, msg="获取成功")


@router.get("/detail/{pk}", response_model=ApiResponse[CaseQcResponse], summary="获取案情质量表详情")
async def get_case_qc(pk: int, db: AsyncSession = Depends(get_db)):
    result = await CaseQcService.get_by_id(db, pk)
    return api_success(data=result, msg="获取成功")


@router.post("/create", response_model=ApiResponse[CaseQcResponse], status_code=status.HTTP_201_CREATED, summary="创建案情质量表")
async def create_case_qc(body: CaseQcCreate, db: AsyncSession = Depends(get_db)):
    result = await CaseQcService.create(db, body)
    return api_success(data=result, msg="创建成功")


@router.put("/update/{pk}", response_model=ApiResponse[CaseQcResponse], summary="更新案情质量表")
async def update_case_qc(pk: int, body: CaseQcUpdate, db: AsyncSession = Depends(get_db)):
    result = await CaseQcService.update(db, pk, body)
    return api_success(data=result, msg="更新成功")


@router.delete("/delete/{pk}", response_model=ApiResponse[dict], summary="删除案情质量表")
async def delete_case_qc(pk: int, db: AsyncSession = Depends(get_db)):
    ok = await CaseQcService.delete(db, pk)
    return api_success({"success": ok}, msg="删除成功" if ok else "未删除")
